package com.loyal.camerax.ui.camera

import android.app.Activity
import android.content.Intent
import android.content.res.Configuration
import android.os.Bundle
import android.util.Log
import androidx.annotation.IdRes
import androidx.appcompat.app.AppCompatActivity
import com.loyal.camerax.databinding.ActivityCameraNavigationBinding
import com.loyal.camerax.utils.ActivityHelper.screenOrientation
import com.loyal.camerax.utils.PreferenceHelper

open class CameraFragmentActivity : AppCompatActivity() {
    private val binding by lazy { ActivityCameraNavigationBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val orientationMutable =
            intent.getBooleanExtra(PreferenceHelper.MUTABLE_ORIENTATION, false)
        Log.e(TAG, "屏幕方向是否可变: $orientationMutable")
        if (orientationMutable) screenOrientation()
        setContentView(binding.root)
    }

    override fun onConfigurationChanged(newConfig: Configuration) {
        super.onConfigurationChanged(newConfig)
        Log.e(TAG, "onConfigurationChanged: ")
    }

    companion object {
        private const val TAG = "CameraFragmentActivity"
        const val CAMERA_ID_RES = "cameraIdRes"

        @JvmStatic
        fun launch(
            activity: Activity, mutableOrientation: Boolean = false, @IdRes cameraIdRes: Int? = -1
        ) {
            val intent = Intent(activity, CameraFragmentActivity::class.java)
            intent.putExtra(PreferenceHelper.MUTABLE_ORIENTATION, mutableOrientation)
            intent.putExtra(CAMERA_ID_RES, cameraIdRes)
            activity.startActivity(intent)
        }
    }
}